package com.xmb.wechat.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ScreenUtils;

/**
 * @Author lijiangop
 * @CreateTime 2020/6/5 15:34
 */
public class MoveLinearLayout extends LinearLayout {

    private int previousX;//初始X轴坐标
    private int previousY;//初始Y轴坐标

    private long actionUpTime;//手指抬起的时间
    private long actionDownTime;//手指按下的时间

    private boolean isOnlyVerticalMove = true;//限制竖直方向移动

    private int mTopMargin    = 0;//距离顶部的最小位置(移动的最顶端)
    private int mBottomMargin = 0;//距离底部的最小位置(移动的最底端)

    private onClickListener onClickListener;

    private static final String TAG = "MoveLinearLayout";
    private              int    mBottom;

    public MoveLinearLayout(Context context) {
        this(context, null);
    }

    public MoveLinearLayout(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public MoveLinearLayout(Context context, AttributeSet attributeSet, int defStyleAttr) {
        super(context, attributeSet, defStyleAttr);
        initFiled();
    }

    private void initFiled() {
        mBottom = ScreenUtils.getScreenHeight() - BarUtils.getNavBarHeight() - BarUtils.getStatusBarHeight();
    }

    public void setOnClickListener(onClickListener listener) {
        this.onClickListener = listener;
    }

    public interface onClickListener {
        void onClick();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int currentX = (int) event.getX();//实时触碰的X轴坐标
        int currentY = (int) event.getY();//实时触碰的Y轴坐标
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                previousX = currentX;//记录初始X轴坐标
                previousY = currentY;//记录初始Y轴坐标
                actionDownTime = System.currentTimeMillis();//记录手指按下的时间
                break;
            case MotionEvent.ACTION_MOVE:
                //限制可拖动的最高位置
                int top = getTop() + (currentY - previousY);
                if (top < mTopMargin) {
                    top = mTopMargin;
                }
                //限制可拖动的最低位置
                int height = getHeight();
                if (top + height > mBottom - mBottomMargin) {
                    top = mBottom - mBottomMargin - height;
                }
                //绘制移动后的位置
                if (isOnlyVerticalMove) {
                    layout(getLeft(), top, getRight(), top + height);//根据坐标重新放置当前View的位置
                } else {
                    int offsetX = currentX - previousX;//计算X轴方向偏移
                    layout(getLeft() + offsetX, top, getRight() + offsetX, top + height);//根据坐标重新放置当前View的位置
                }
                break;
            case MotionEvent.ACTION_UP:
                //                displayMetrics = getResources().getDisplayMetrics();
                //                windowWidth = displayMetrics.widthPixels;//获取屏幕宽度
                //                int finalLeftDistance = getLeft();

                //                if (finalLeftDistance > (windowWidth - getWidth()) / 2) {//以屏幕中间为界限，平滑回到水平方向的右边
                //                    //ValueAnimator数值发生器，产生有一定规律的数值，从而控制动画实现
                //                    ValueAnimator valueAnimator = ValueAnimator.ofInt(getLeft(), windowWidth - 100 - getWidth());
                //                    valueAnimator.setDuration(500);
                //                    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                //                        @Override
                //                        public void onAnimationUpdate(ValueAnimator animation) {
                //                            int smoothX = (int) animation.getAnimatedValue();
                //                            layout(smoothX, getTop(), smoothX + getWidth(), getBottom());
                //                        }
                //                    });
                //                    valueAnimator.start();
                //                } else if (finalLeftDistance < (windowWidth - getWidth()) / 2) {//以屏幕中间为界限，平滑回到水平方向的左边
                //                    ValueAnimator valueAnimator = ValueAnimator.ofInt(getLeft(), 100);
                //                    valueAnimator.setDuration(500);
                //                    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                //                        @Override
                //                        public void onAnimationUpdate(ValueAnimator animation) {
                //                            int smoothX = (int) animation.getAnimatedValue();
                //                            layout(smoothX, getTop(), smoothX + getWidth(), getBottom());
                //                        }
                //                    });
                //                    valueAnimator.start();
                //                }

                actionUpTime = System.currentTimeMillis();//记录手指抬起的时间
                long intervalTime = actionUpTime - actionDownTime;
                if (intervalTime < 200) { //判断按下抬起的时间间隔，点击回调解决TouchEvent和点击事件冲突的问题
                    onClickListener.onClick();
                }
                break;
        }
        return true;
    }
}
